import loki from 'lokijs'

import LokiIndexedAdapter from 'lokijs/src/loki-indexed-adapter'

const idbAdapter = new LokiIndexedAdapter('finance')
// // use paging only if you expect a single collection to be over 50 megs or so
const pa = new loki.LokiPartitioningAdapter(idbAdapter, { paging: true })
let chart = null

const db = new loki('example.db', {
  adapter: pa,
  autoload: true,
  autosave: true,
  autosaveInterval: 2000,
  autoloadCallback: () => {
    // check collection
    if (db.getCollection('chart3')) {
      chart = db.getCollection('chart3')
    } else {
      chart = db.addCollection('chart3')
    }
  }
})

// // check collection
// if (db.getCollection('chart3')) {
//   chart = db.getCollection('chart3')
// } else {
//   chart = db.addCollection('chart3')
// }

export default class DaxieAdaptor {
  init() {
    // clear all
    if (chart) chart.clear()
  }

  query(...args) {
    return chart.findOne(...args)
  }

  insert(...args) {
    return chart.insert(...args)
  }

  insertMany(...args) {
    return this.insert(...args)
  }

  getAll() {
    return chart.toArray()
  }
}
